<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    request.setAttribute("basePath", basePath);
%>
<%@ include file="/resources/js/easyui/taglibs.jsp" %>
<%@ include file="/resources/js/wechat/_wechatui.jsp" %>
<%@ include file="/resources/js/easyui/_viewJS.jsp" %>
<!DOCTYPE>
<html>
<head>
    <title>修改密码</title>
    <script src="${basePath}resources/js/Jquery-md5.js"></script>
</head>
<body>
<div class="loading"></div>
<!-- header -->
<%--<header class='login_header'>
    <a href="javascript:history.go(-1);" class="top_fh">微信</a>
</header>--%>
<!-- header end -->

<div class="page login_page">
    <div class="login_inner">
        <div class="zh_title"><em class="sjzh_tile">修改密码</em></div>
        <!-- 错误提示 -->
        <div class="loginPoint"></div>
        <div class="lgin_form">
            <div class='lg_ps'><input id="password" name="password" type="password" autofocus class="sjzhresetpsw"
                                      placeholder="请输入新密码"></div>
            <div class='lg_qrmm'><input id="npassword" name="npassword" type="password" class="sjzhpsw"
                                        placeholder="重新输入密码"></div>
            <div class='lg_yzm'><input id="code" type="text" placeholder="请输入验证码" class="sjzhyzm"><img id="captchaImage"
                                                                                                       onclick="chageCode()"
                                                                                                       src="${basePath}authCode"
                                                                                                       alt=""></div>
            <div class='lg_sjyzm'><input id="mobilecode" name="validCode" type="text" class="sjzhpswTel"
                                         placeholder="请输入验证码"><a href="JavaScript:;" class="hqzma">获取验证码</a></div>
            <div class="lg_button"><input type="button" value="确定" class="lg_btn3"></div>
        </div>
    </div>
    <div class="footer_bz">游艺云</div>
</div>
</body>
</html>

<script>
    function chageCode() {
        $('#captchaImage').attr('src', '${basePath}authCode?abc=' + Math.random());//链接后添加Math.random，确保每次产生新的验证码，避免缓存问题。
    }
    $(function () {
        $(".page").yanZheng();
        var regPassword = /^\w{6,18}$/;
        var code = "";
        var mobilecode = "";
        var password = "";
        var npassword = "";
        //验证码倒计时
        var setTime = 0;

        function yzsetTime($index) {
            var yzmset = window.setInterval(function () {
                setTime++;
                if (setTime == 60) {
                    setTime = 0;
                    $($index).removeClass('cur').text('获取验证码');
                    getSMScode();
                    clearInterval(yzmset);
                } else {
                    $($index).text('重新发送（' + (60 - setTime) + 's）');
                }
            }, 1000);
        }

        //获取验证码
        var getSMScode = function () {
            $(".lg_sjyzm a").on("click", function () {
                password = $("#password").val();
                npassword = $("#npassword").val();
                code = $("#code").val();
                if (password == null || password == '') {
                    //alert("密码不能为空！");
                    $(".loginPoint").text("密码不能为空");
                    return;
                }
                if (npassword == null || npassword == '') {
                    //alert("密码不能为空！");
                    $(".loginPoint").text("确认密码不能为空");
                    return;
                }
                if (npassword != password) {
                    //alert("两次输入密码不一致！");
                    $(".loginPoint").text("两次输入密码不一致");
                    return;
                }
                if (code == null || code == '') {
                    $(".loginPoint").text("验证码不能为空");
                    //alert("验证码不能为空！");
                    return;
                }
                if (!$(this).hasClass('cur')) {
                    $(this).addClass('cur').text('重新发送（60s）');
                    $(this).unbind("click");
                    yzsetTime($(this));
                }

                $.ajax({
                    url: '${basePath}sms/send',
                    type: 'POST',
                    data: 'validCode=' + code + "&userId=${userId}",
                    error: function (response) {
                        ViewHelper.func.ajaxWechatErrorMessage(response);
                    },
                    success: function (ret) {
                        if (ret.result) {
                            //alert(ret.info);
                            console.log(ret.info);
                            $(".loginPoint").text("短信已发送至您的手机，请查收！").css("color", "#FFF");
                            //window.location = "${basePath}wechat/income/accounts/mainView";
                        } else {
                            //alert(ret.info);
                            $(".loginPoint").text(ret.info).css("color", "#ab1e1e");
                        }
                    }
                });
            });
        }

        //登录
        $(".lg_button input").on("click", function () {
            password = $("#password").val();
            npassword = $("#npassword").val();
            code = $("#code").val();
            mobilecode = $("#mobilecode").val();
            if (password == null || password == '') {
                $(".loginPoint").text("密码不能为空");
                //alert("密码不能为空！");
                return;
            }
            if (npassword == null || npassword == '') {
                $(".loginPoint").text("确认密码不能为空");
                //alert("密码不能为空！");
                return;
            }
            if (npassword != npassword) {
                $(".loginPoint").text("两次输入密码不一致");
                //alert("两次输入密码不一致！");
                return;
            }
            if (code == null || code == '') {
                $(".loginPoint").text("验证码不能为空");
                //alert("验证码不能为空！");
                return;
            }

            if (mobilecode == null || mobilecode == '') {
                $(".loginPoint").text("手机验证不能为空");
                //alert("手机验证不能为空！！");
                return;
            }
            if (regPassword.test(password) && npassword == password && code != "" && mobilecode != "") {
                console.log(createPassword(password, password));
                $.ajax({
                    url: '${basePath}wechat/login/ep',
                    type: 'POST',
                    data: {
                        'userId':${userId},
                        'password': createPassword(password, password),
                        'npassword': createPassword(npassword, npassword),
                        'validCode': mobilecode
                    },
                    error: function (response) {
                        ViewHelper.func.ajaxWechatErrorMessage(response);
                    },
                    success: function (ret) {
                        if (ret.result) {
                            //$(".loginPoint").text(ret.info);
                            alert(ret.info);
                            window.location = "${basePath}wechat/login/gotoSignIn";
                        } else {
                            //alert(ret.info);
                            $(".loginPoint").text(ret.info);
                        }
                    }
                });
            }
        });

        getSMScode();
    });

</script>

